package net.m510.autosurfer.saison;

import java.io.File;
import java.net.URI;

import net.m510.autosurfer.common.FileGetProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;

import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class SaisonDownload extends FileGetProcess {
	public SaisonDownload() {
		this(null,null,null);
	}
	
	public SaisonDownload(CloseableHttpClient client, HttpClientContext context, File dir) {
		super(client, context, dir, null);
	}

	@Override
	protected URI getUri(Document doc) {
		Elements e = doc.getElementsByAttributeValue("alt", "CSVダウンロード");
		if ( e.isEmpty() ) throw new InvalidDocumentException("CSVダウンロードのURLが見つかりません",doc);
		HttpHost host = context.getTargetHost();
		URI uri = URI.create(host.toURI());
		uri = uri.resolve(e.first().parent().attr("href"));
		return uri;
	}
}
